package com.ruoyi.web.controller.system;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SequenceUtils;
import com.ruoyi.system.domain.TGoods;
import com.ruoyi.system.domain.TOrders;
import com.ruoyi.system.domain.TOrdersItems;
import com.ruoyi.system.service.ITGoodsService;
import com.ruoyi.system.service.ITOrdersItemsService;
import com.ruoyi.system.service.ITOrdersService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TGoodsCarts;
import com.ruoyi.system.service.ITGoodsCartsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * t_goods_carts 商品购物车主Controller
 * 
 * @author ruoyi
 * @date 2022-04-09
 */
@RestController
@RequestMapping("/system/carts")
public class TGoodsCartsController extends BaseController
{
    @Autowired
    private ITGoodsCartsService tGoodsCartsService;


    @Autowired
    private ITOrdersService itOrdersService;

    @Autowired
    private ITGoodsService itGoodsService;

    @Autowired
    private ITOrdersItemsService itOrdersItemsService;

    /**
     * 查询t_goods_carts 商品购物车主列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TGoodsCarts tGoodsCarts)
    {
        startPage();
        List<TGoodsCarts> list = tGoodsCartsService.selectTGoodsCartsList(tGoodsCarts);
        return getDataTable(list);
    }

    /**
     * 新增t_goods_carts 商品购物车主
     */
    @PostMapping
    public AjaxResult add(@RequestBody TGoodsCarts tGoodsCarts)
    {
        if(null==tGoodsCarts.getGoodsId()){
            return AjaxResult.error("参数错误!");
        }
        return toAjax(tGoodsCartsService.insertTGoodsCarts(tGoodsCarts));
    }

    /**
     * 修改t_goods_carts 商品购物车主
     */
    @PutMapping
    public AjaxResult edit(@RequestBody TGoodsCarts tGoodsCarts)
    {
        return toAjax(tGoodsCartsService.updateTGoodsCarts(tGoodsCarts));
    }

    /**
     * 删除t_goods_carts 商品购物车主
     */
    @DeleteMapping("/{pkIds}")
    public AjaxResult remove(@PathVariable Long[] pkIds)
    {
        return toAjax(tGoodsCartsService.deleteTGoodsCartsByPkIds(pkIds));
    }

    @PostMapping("/addOrder/{pkIds}")
    @Transactional
    public AjaxResult addOrder(@PathVariable Long[] pkIds)
    {
        TOrders orders=new TOrders();
        orders.setOrderId(itOrdersService.getOrderId());
        orders.setCreatedTime(DateUtils.getNowDate());
        BigDecimal total=new BigDecimal(0.0);
        for(Long pkId:pkIds){
           TGoodsCarts goodsCarts= tGoodsCartsService.selectTGoodsCartsByPkId(pkId);
            TGoods goods=itGoodsService.selectTGoodsByGoodsId(goodsCarts.getGoodsId());
            TOrdersItems ordersItems=new TOrdersItems();
            ordersItems.setOrderId(orders.getOrderId());
            ordersItems.setGoodsId(goods.getGoodsId());
            ordersItems.setCreatedTime(DateUtils.getNowDate());
            ordersItems.setGoodsName(goods.getName());
            ordersItems.setOrderType(goodsCarts.getGdsQty().intValue()+"");
            ordersItems.setUnitPrice(BigDecimalUtil.mul(Double.valueOf(goodsCarts.getGdsQty()),goods.getVipPrice().doubleValue()));
            itOrdersItemsService.insertTOrdersItems(ordersItems);
            goodsCarts.setDeleteFlag("1");
            tGoodsCartsService.updateTGoodsCarts(goodsCarts);
            total=BigDecimalUtil.add(total.doubleValue(),ordersItems.getUnitPrice().doubleValue());
        }
        orders.setOrderType(1);
        orders.setTotalFeePaid(total);
        orders.setStatus(1);
        orders.setCreatedTime(DateUtils.getNowDate());
        orders.setUpdatedTime(DateUtils.getNowDate());
        itOrdersService.insertTOrders(orders);
        return AjaxResult.success("操作成功",orders.getOrderId());
    }




}
